#include "dialog_camadjust.h"
#include "ui_dialog_camadjust.h"
#include <QDebug>
#include <QPushButton>
#include <QPushButton>


int Dialog_camAdjust::topCamadjustValue = 740;
int Dialog_camAdjust::sideCamadjustValue = 740;

Dialog_camAdjust::Dialog_camAdjust(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog_camAdjust)
{
    ui->setupUi(this);

//    ui->label_MinVal->setText(QString::number(ui->hSlider_currentAngle->minimum()));
//    ui->label_MaxVal->setText(QString::number(ui->hSlider_currentAngle->maximum()));

//    // 创建文本输入框（用于显示和输入数字）
//    ui->lineEdit_angle->setValidator(new QIntValidator(ui->hSlider_currentAngle->minimum(), ui->hSlider_currentAngle->maximum(), this)); // 限制输入范围

    ui->hSlider_currentAngle->setValue(topCamadjustValue);

//    // 角度+
//    connect(ui->btn_angle_add, &QPushButton::clicked, this, [=](){

//        m_angle = ui->hSlider_singleAngleStep->value();/*ui->lineEdit_angle->text().toUInt();*/

//        int16_t currentAngle = ui->hSlider_currentAngle->value();

//        ui->hSlider_currentAngle->setValue(currentAngle + m_angle);


//       this->SetAngle(m_angle);
//    });

//    // 角度-
//    connect(ui->btn_angle_subtract, &QPushButton::clicked, this, [=](){
//        int16_t Angle = ui->lineEdit_angle->text().toUInt();

//        m_angle = -Angle;

//        int16_t currentAngle = ui->hSlider_currentAngle->value();

//        ui->hSlider_currentAngle->setValue(currentAngle - Angle);


//        this->SetAngle(m_angle);
//    });

//    ui->label_angle->setNum(ui->hSlider_currentAngle->value());

    connect(ui->hSlider_currentAngle, &QSlider::valueChanged, [=](int value){
//        ui->label_angle->setNum(value);

        QString currentText = ui->comboBox_Lens_name->currentText();
        if(currentText == "顶部")
        {
            topCamadjustValue = ui->hSlider_currentAngle->value();
        } else if(currentText == "侧面")
        {
            sideCamadjustValue = ui->hSlider_currentAngle->value();
        }

        qDebug() << "当前：" << value << "顶部：" << topCamadjustValue << "侧面：" << sideCamadjustValue;
    });


    // SetAngle(topCamadjustValue);


    connect(ui->comboBox_Lens_name, &QComboBox::currentTextChanged, [=](QString currentText){
        int CamadjustValue = ui->hSlider_currentAngle->value();
//        int currentAngle = ui->hSlider_currentAngle->value();

        if(currentText == "顶部")
        {
            CamadjustValue = topCamadjustValue;

        } else if(currentText == "侧面")
        {
            CamadjustValue = sideCamadjustValue;
        }

//        if(currentAngle < CamadjustValue)
//        {
//            currentAngle = CamadjustValue - CamadjustValue;
//        } else {
//            currentAngle = -(currentAngle - CamadjustValue);
//        }

//        SetAngle(currentAngle);
        ui->hSlider_currentAngle->setValue(CamadjustValue);
    });
}

Dialog_camAdjust::~Dialog_camAdjust()
{
    delete ui;
}

void Dialog_camAdjust::do_btn_angle_add(const uint16_t angleAdd)
{
    m_angle = ui->hSlider_singleAngleStep->value();/*ui->lineEdit_angle->text().toUInt();*/

    int16_t currentAngle = ui->hSlider_currentAngle->value();

    ui->hSlider_currentAngle->setValue(currentAngle + m_angle);


   this->SetAngle(m_angle);
}


void Dialog_camAdjust::do_btn_angle_subtract(const uint16_t angleSub)
{
    int16_t Angle = ui->hSlider_singleAngleStep->value(); /*ui->lineEdit_angle->text().toUInt();*/

    m_angle = -Angle;

    int16_t currentAngle = ui->hSlider_currentAngle->value();

    ui->hSlider_currentAngle->setValue(currentAngle - Angle);

    this->SetAngle(m_angle);
}


void Dialog_camAdjust::SetAngle(int16_t angle)
{
    QByteArray dat = formSetData(m_speed,angle,m_radio);

    if(ui->comboBox_Lens_name->currentText() == "侧面")
    {
        emit sendSideAdjustData(dat);
    } else {
        emit sendTopAdjustData(dat);
    }

    do_disableAngleSet(false);
}


void Dialog_camAdjust::getTopRecv(QByteArray data)
{
    QString dat_str = "Top 镜头->返回 Data(HEX):";

    for(int i = 0; i < data.size(); ++i)
    {
        dat_str += QString("%1 ").arg(data.at(i), 2, 16, QChar('0')).toUpper();
    }

    // 判断响应是否到位置
    if(ui->comboBox_Lens_name->currentText() == "顶部")
    {
        do_disableAngleSet(true);
    }
}

void Dialog_camAdjust::getSideRecv(QByteArray data)
{
    QString dat_str = "Side 镜头->返回 Data(HEX):";

    for(int i = 0; i < data.size(); ++i)
    {
        dat_str += QString("%1 ").arg(data.at(i), 2, 16, QChar('0')).toUpper();
    }
    // 判断响应是否到位置
    if(ui->comboBox_Lens_name->currentText() == "侧面")
    {
        do_disableAngleSet(true);
    }}


QByteArray Dialog_camAdjust::formSetData(int speed,int angle,int radio)
{
    QByteArray dat;
    dat.append(0x01);
    dat.append(0x64);
    dat.append(0x01);
    int temp = speed * radio /6000;
    dat+=intToByteArray(temp);
    temp = angle * radio /360;
    dat+=intToByteArray(temp);

    dat.append('\x00');
    dat.append('\x00');
    dat.append('\x00');
    return dat;
}
QByteArray Dialog_camAdjust::intToByteArray(int value)
{
    QByteArray byteArray;
    byteArray.resize(4);

    byteArray[0] = (uint8_t)(value >> 24) & 0xFF;
    byteArray[1] = (uint8_t)(value >> 16) & 0xFF;
    byteArray[2] = (uint8_t)(value >> 8) & 0xFF;
    byteArray[3] = (uint8_t) value & 0xFF;

    return byteArray;
}
void Dialog_camAdjust::on_btn_ok_clicked()
{
    accept();
}

void Dialog_camAdjust::on_btn_cancel_clicked()
{
    reject();
}

void Dialog_camAdjust::do_disableAngleSet(bool state)
{
    ui->btn_setCurrentAngle->setEnabled(state);
    ui->btn_setSingAngleStep->setEnabled(state);
}

void Dialog_camAdjust::on_btn_angle_subtract_clicked()
{

}

void Dialog_camAdjust::on_btn_angle_add_clicked()
{

}

void Dialog_camAdjust::mousePressEvent(QMouseEvent *event)
{
    // 调用基类的 mousePressEvent 以保持默认行为（如果需要）
    QDialog::mousePressEvent(event);
}
